package com.example.base.service;

import com.example.base.entity.User;
import com.example.base.entity.UserCode;
import com.example.base.entity.UserDormRelation;
import com.example.base.entity.UserInfo;
import com.example.base.mapper.UserCodeMapper;
import com.example.base.mapper.UserDormRelationMapper;
import com.example.base.mapper.UserInfoMapper;
import com.example.base.mapper.UserMapper;
import com.example.base.utils.Md5Generator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    UserInfoMapper userInfoMapper;

    @Autowired
    UserCodeMapper userCodeMapper;

    @Autowired
    UserDormRelationMapper userDormRelationMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public void cacheEmail(String email) {
        redisTemplate.opsForValue().set(email, true);
    }

    public void cacheStudentNo(String studentNo) {
        redisTemplate.opsForValue().set(studentNo, true);
    }

    public boolean hasEmail(String email) {
        return redisTemplate.opsForValue().get(email) != null;
    }

    public boolean hasStudentNo(String studentNo) {
        return redisTemplate.opsForValue().get(studentNo) != null;
    }

    public void cacheUser(User user) {
        redisTemplate.opsForValue().set("no2user:" + user.getUserInfo().getStudentNo(), user);
        redisTemplate.opsForValue().set("email2user:" + user.getEmail(), user);
        System.out.println("cacheUser user = " + user);
    }

    public void cacheUserCode(Integer uid, String code) {
        redisTemplate.opsForValue().set("uid2code:" + uid, code);
    }

    public void cacheUserInfoById(Integer id, UserInfo userInfo) {
        redisTemplate.opsForValue().set("uid2info:" + id, userInfo);
    }

    public UserDormRelation getRelationByUid(String studentNo) {
        return ((UserDormRelation) redisTemplate.opsForValue().get("no2relation:" + studentNo));
//        return userDormRelationMapper.getRelationByUid(uid);
    }

    public UserInfo getUserInfoById(Integer id) {
        return userInfoMapper.getInfoById(id);
    }

    public void insertUserCode(Integer uid, String uuid) {
        userCodeMapper.insertUserCode(uid, uuid);
    }

    public void insertUserInfo(UserInfo info) {
        userInfoMapper.insertUserInfo(info);
    }

    public UserInfo getUserInfoByStudentNo(String studentNo) {
        User user = (User) redisTemplate.opsForValue().get("no2user:" + studentNo);
        if (user == null) return null;
        return user.getUserInfo();
        //return userInfoMapper.getInfoByStudentNo(studentNo);
    }

    public String getUserCodeByUid(Integer uid) {
        return ((String) redisTemplate.opsForValue().get("uid2code:" + uid));
//        return userCodeMapper.getUserCodeByUid(uid);
    }

    public List<User> getAllUser() {
        return userMapper.getAllUser();
    }

    public void insertUser(User user, Integer uid) {
        userMapper.insertUser(user, uid);
    }

    public User getUserByEmail(String email) {
        return (User) redisTemplate.opsForValue().get("email2user:" + email);
//        return userMapper.getUserByEmail(email);
    }

    public boolean isLoginSuccess(User user) {
        User u = getUserByEmail(user.getEmail());
        return u != null && u.getPassWord().equals(Md5Generator.getMD5String(user.getPassWord()));
    }
}